<template>
	<view class="card">
		<view class="card-box"  @tap="cardClick">
			<image class="card-box-image" src="/static/image/icon-dop.png" mode=""></image>
			<view class="card-box-title">
				{{cardIndex == 0?'上班打卡':cardIndex === 2?'打卡完毕':cardIndex==4?'正在外勤':'下班打卡'}}
			</view>
			<view class="card-box-time">
				{{formatDate}}
			</view>
		</view>
		<view class="card-title">当日打卡时间记录和位置</view>
		<view class="bottom" v-if="gooutid!=''">
			<button type="warn" size="mini" @tap="endoutWork">取消外出</button>
		</view>
		<view v-else>
		<view class="card-info" v-show="cardIndex !== 0" >
			<view class="card-info-item" v-show="cardIndex === 1 || cardIndex === 2">
				<uni-card 
					is-full="true" 
					title="上班打卡" 
					thumbnail="/static/image/check.png" 
					:extra="cardInfoArr.hr_attend_workingState"
					:note="'ip地址：'+cardInfoArr.hr_attend_workIp">
					<view class="uni-card-time">
						上班打卡时间{{cardInfoArr.hr_attend_startWork}}
					</view>
					<view class="uni-card-text">
						{{cardInfoArr.hr_attend_workAddress}}
					</view>
				</uni-card>
			</view>
			<view class="card-info-item" v-show="cardIndex === 2">
				<uni-card 
					is-full="true" 
					title="下班打卡" 
					thumbnail="/static/image/check.png" 
					:extra="cardInfoArr.hr_attend_restState"
					:note="'ip地址：'+cardInfoArr.hr_attend_underIp">
					<view class="uni-card-time">
						下班打卡时间{{cardInfoArr.hr_attend_knockOff}}
					</view>
					<view class="uni-card-text">
						{{cardInfoArr.hr_attend_offWorkAddress}}
					</view>
				</uni-card>
			</view>
		</view>
		</view>
	</view>
</template>

<script>
	import uniCard from "@/components/uni-card/uni-card.vue"
	import {apiGetClock,apiGetOffWork,apiendOutWork} from "@/common/api.js"
	export default{
		name:"clock-card",
		components:{
			uniCard
		},
		props:{
			gooutid:String,
			address:String,
			cardInfoArr:Object,
			cardIndex:Number,
			addressCode:Number
		},
		data(){
			return {
				formatDate:'00:00:00'
			}
		},
		created() {
			this.getTime()
			setInterval(()=>{
				this.getTime()
			},1000)
		},
		methods:{
			endoutWork(){
				apiendOutWork({
					field_personnel_id:this.gooutid
				}).then(res=>{
					// uni.showToast({
					// 	title: '外勤状态结束',
					// 	duration: 2000
					// });
					this.$emit("setgooutid",'')
				})
			},
			getTime(){
				let date = new Date()
				let h = date.getHours()
				let m = date.getMinutes()
				let s = date.getSeconds()
				if (h < 10) {
				  h = '0' + h
				}
				if (m < 10) {
				  m = '0' + m
				}
				if (s < 10) {
				  s = '0' + s
				}
				this.formatDate = h + ':' + m + ':' + s
			},
			cardClick(){
				let that = this
				// let date = new Date()
				// let h = date.getHours()
				if(that.cardIndex == 2){
					that.toast('你今日已打卡')
					return
				}
				uni.showModal({
					title: '操作提示',
					content: '当前位置是：'+that.address+' ,确定打卡吗？',
					showCancel: true,
					success: res => {
						uni.showLoading({
							title: '加载中...',
							mask: true
						})
						
						if(res.confirm){
							if(that.addressCode == 1){
								uni.hideLoading()
								that.toast('打卡失败，当前位置不在范围内')
								return
							}
							
							if(that.cardIndex === 0){
								apiGetClock({
									hr_attend_workAddress:that.address,
									hr_attend_workIp:'192.168.11.112'
								}).then((res)=>{
									uni.hideLoading()
									that.toast('打卡成功','success')
									that.$emit('change')
								}).catch((res)=>{
									uni.hideLoading()
									that.toast(res.msg)
								})
							}else{
								// if(h < 17){
								// 	that.toast('打卡失败，未到打卡时间')
								// 	return
								// }
								apiGetOffWork({
									hr_attend_offWorkAddress:that.address,
									hr_attend_workIp:'192.168.11.112'
								}).then((res)=>{
									that.toast('打卡成功','success')
									that.$emit('change')
								}).catch((res)=>{
									that.toast(res.msg)
								})
							}
						}else{
							uni.hideLoading()
						}
					}
				});
			},
			toast(title,icon='none'){
				uni.showToast({
					title: title,
					mask: true,
					icon:icon,
					duration: 1500
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.card{
		margin: 20upx auto;
		min-height: 1000upx;
		background-color: #fff;
		.card-title{
			width: 100%;
			text-align: center;
			font-size: $uni-font-size-lg;
			color:$uni-text-color-placeholder;
			font-weight: bold;
		}
	}
	.card-box{
		width: 386upx;
		height: 386upx;
		margin: 0 auto;
		position: relative;
		&-image{
			width: 100%;
			height: 100%;
		}
		&-title{
			position: absolute;
			top: 30%;
			color: #fff;
			left: 24%;
			font-size:50upx;
		}
		&-time{
			position: absolute;
			top: 50%;
			color: #a1ddff;
			left: 30%;
			font-size:40upx;
		}
	}
	.card-info{
		margin: 30upx;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		&-item{
			width: 100%;
			margin-bottom: 30upx;
		}
	}
	.uni-card-time{
		padding: 10upx 0;
		font-size:$uni-font-size-lg;
		font-weight:bold;
	}
	.uni-card-text{
		font-size:$uni-font-size-lg;
	}
	.bottom{
		display: flex;
		align-items: center;
		justify-content:center;
		margin-top:50upx;
	}
</style>
